package com.oa.tools.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.oa.tools.dao.PersonDAO;
import com.oa.tools.jobs.DynamicJob;
import com.oa.tools.utils.DateUtil;
import com.oa.tools.utils.ExcelUtil;
import com.oa.tools.utils.StringUtil;

@RestController
@RequestMapping(path = "fee")
public class OAExcepseQueryController {
	
	@Autowired
	PersonDAO personDAO;
	
	public static final Logger logger = LoggerFactory.getLogger(DynamicJob.class);
	
	@RequestMapping({ "export" })
	public void allFee(HttpServletRequest request,HttpServletResponse response/*@RequestBody Map<String,Object> param*/) {
		try {
			
			String titles [] = new String[] {"报销日期","单据编号","报销人","费用归属部门","费用项目","金额"};
			
			List<Map<String,Object>> dataMaps = personDAO.selectFinishedExpenseBySql();
			
			List<List<String>> datas = new ArrayList<>();
			List<String> titleList = new ArrayList<>(); 
			for(String title:titles) {
				titleList.add(title);
			}
			datas.add(titleList);
			
			
			for(Map<String,Object> lineMap:dataMaps) {
				List<String> lineDataList = new ArrayList<>(); 
				for(String title:titles) {
					lineDataList.add(StringUtil.parseString(lineMap.get(title)));
				}
				datas.add(lineDataList);
			}
			ExcelUtil.exportExcel(response, "已完成报销数据导出.xls", datas);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
	}
	
	
	@RequestMapping({ "wyc/export" })
	public void wycFee(HttpServletRequest request,HttpServletResponse response/*@RequestBody Map<String,Object> param*/) {
		try {
			
	//		logger.info("callback received oa flow notice callback message:"+param.toString());
	//		FileUtil.writeTxt2File(param.toString(), SysConfig.getOutPath());
			
			String titles [] = new String[] {"申请部门","处理人","审批状态","付款日期","费用类型","费用开始日期","费用结束日期",	"本币金额","单据编号","归属期间","核准申报金额","申请日期","用途摘要","申请人","报销内容"};
			
			List<Map<String,Object>> dataMaps = personDAO.selectWYCExpenseBySql();
			
			List<List<String>> datas = new ArrayList<>();
			List<String> titleList = new ArrayList<>(); 
			for(String title:titles) {
				titleList.add(title);
			}
			datas.add(titleList);
			
			
			for(Map<String,Object> lineMap:dataMaps) {
				List<String> lineDataList = new ArrayList<>(); 
				for(String title:titles) {
					lineDataList.add(StringUtil.parseString(lineMap.get(title)));
				}
				datas.add(lineDataList);
			}
			ExcelUtil.exportExcel(response, "网约车报销数据导出.xls", datas);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
	}
	
	
	@RequestMapping({ "wyc/publish/export/{fdName}" })
	public void feePublishedList(HttpServletRequest request,HttpServletResponse response,@PathVariable(name="fdName") String fdName) throws Exception {
		
		String titles [] = new String[] {"报销名称","单据编号","报销人","费用归属部门","报销日期","收款人帐号","收款人名称","金额","财务付款日期","流程结束日期","审批状态"};
		
		List<Map<String,Object>> dataMaps = personDAO.selectPublishedFee(fdName);
		
		List<List<String>> datas = new ArrayList<>();
		List<String> titleList = new ArrayList<>(); 
		for(String title:titles) {
			titleList.add(title);
		}
		datas.add(titleList);
		
		for(Map<String,Object> lineMap:dataMaps) {
			List<String> lineDataList = new ArrayList<>(); 
			for(int i = 0;i<titles.length;i++) {
				String title = titles[i];
				lineDataList.add(StringUtil.parseString(lineMap.get(title)));
			}
			datas.add(lineDataList);
		}
		
		ExcelUtil.exportExcel(response, fdName+"报表记录"+DateUtil.curDayStr()+".xls", datas);
		
	}
	
	@RequestMapping({ "wyc/payinfo/cmb/export" })
	public void cmbPayInfo(HttpServletRequest request,HttpServletResponse response/*@RequestBody Map<String,Object> param*/) throws Exception {
		
		String titles [] = new String[] {
				"业务参考号","收款人编号","收款人帐号","收款人名称","收方开户支行","收款人所在省","收款人所在市","收方邮件地址",
				"收方移动电话","币种","付款分行","结算方式","业务种类","付方帐号","付方虚拟户编号","期望日",
				"期望时间","用途","金额","收方联行号","收方开户银行","业务摘要","发票备注","OA报销单号"
		};
		
		List<Map<String,Object>> dataMaps = personDAO.selectCMBPayInfoBySql();
		
		List<List<String>> datas = new ArrayList<>();
		List<String> titleList = new ArrayList<>(); 
		for(String title:titles) {
			titleList.add(title);
		}
		datas.add(titleList);
		
		
		for(Map<String,Object> lineMap:dataMaps) {
			List<String> lineDataList = new ArrayList<>(); 
			for(int i = 0;i<titles.length;i++) {
				String title = titles[i];
				
				if(title.equals("收款人编号")) {
					lineDataList.add(DateUtil.curDayStr()+StringUtil.getPinyin(StringUtil.parseString(lineMap.get("收款人名称")))+"-"+i);
				}else {
					lineDataList.add(StringUtil.parseString(lineMap.get(title)));
				}
			}
			datas.add(lineDataList);
		}
		
		ExcelUtil.exportExcel(response, "招行付款信息导出"+DateUtil.curDayStr()+".xls", datas);
		
		
	}
	
	@RequestMapping({ "wyc/payinfo/shanghai/export" })
	public void shanghaiPayInfo(HttpServletRequest request,HttpServletResponse response/*@RequestBody Map<String,Object> param*/) throws Exception {
		
		String titles [] = new String[] {"转账类型","付款单位账号","收款单位账号","收款单位名称","收款单位开户行名","收款单位网点名称","收款银行行号","付款金额","付款用途","加急标志","OA报销单号"};
		
		List<Map<String,Object>> dataMaps = personDAO.selectShangHaiPayInfoBySql();
		
		List<List<String>> datas = new ArrayList<>();
		List<String> titleList = new ArrayList<>(); 
		for(String title:titles) {
			titleList.add(title);
		}
		datas.add(titleList);
		
		for(Map<String,Object> lineMap:dataMaps) {
			List<String> lineDataList = new ArrayList<>(); 
			for(int i = 0;i<titles.length;i++) {
				String title = titles[i];
				lineDataList.add(StringUtil.parseString(lineMap.get(title)));
			}
			datas.add(lineDataList);
		}
		
		ExcelUtil.exportExcel(response, "上海付款信息导出"+DateUtil.curDayStr()+".xls", datas);
		
	}
	
	@RequestMapping({ "wyc/export/{feeType}" })
	public void tuanjianList(HttpServletRequest request,HttpServletResponse response,@PathVariable("feeType") String feeType) throws Exception {
		
		String titles [] = new String[] {"申请部门","处理人","付款状态","费用类型","费用开始日期","费用结束日期",	"本币金额","单据编号","归属期间","核准申报金额","申请日期","用途摘要","申请人","报销内容"};
		
		List<Map<String,Object>> dataMaps = personDAO.selectTuanjianPayInfoBySql(feeType);
		
		List<List<String>> datas = new ArrayList<>();
		List<String> titleList = new ArrayList<>(); 
		for(String title:titles) {
			titleList.add(title);
		}
		datas.add(titleList);
		
		for(Map<String,Object> lineMap:dataMaps) {
			List<String> lineDataList = new ArrayList<>(); 
			for(int i = 0;i<titles.length;i++) {
				String title = titles[i];
				lineDataList.add(StringUtil.parseString(lineMap.get(title)));
			}
			datas.add(lineDataList);
		}
		
		ExcelUtil.exportExcel(response, feeType+"报销记录"+DateUtil.curDayStr()+".xls", datas);
		
	}
	
	
	@RequestMapping({ "wyc/jiayou/export" })
	public void fuelChargingList(HttpServletRequest request,HttpServletResponse response/*@RequestBody Map<String,Object> param*/) throws Exception {
		
		String titles [] = new String[] {"报销单号","报销部门","车架号","费用科目","车型","费用","备注"};
		
		List<Map<String,Object>> dataMaps = personDAO.selectFuelChargingListBySql();
		
		List<List<String>> datas = new ArrayList<>();
		List<String> titleList = new ArrayList<>(); 
		for(String title:titles) {
			titleList.add(title);
		}
		datas.add(titleList);
		
		for(Map<String,Object> lineMap:dataMaps) {
			List<String> lineDataList = new ArrayList<>(); 
			for(int i = 0;i<titles.length;i++) {
				String title = titles[i];
				lineDataList.add(StringUtil.parseString(lineMap.get(title)));
			}
			datas.add(lineDataList);
		}
		
		ExcelUtil.exportExcel(response, "新车加油费报销记录"+DateUtil.curDayStr()+".xls", datas);
		
	}
	
	@RequestMapping({ "wyc/xiche/export" })
	public void vehicleCleangingList(HttpServletRequest request,HttpServletResponse response/*@RequestBody Map<String,Object> param*/) throws Exception {
		
		String titles [] = new String[] {"报销单号","报销部门","车架号","费用科目","车型","费用","备注"};
		
		List<Map<String,Object>> dataMaps = personDAO.selectVehicleCleaingListBySql();
		
		List<List<String>> datas = new ArrayList<>();
		List<String> titleList = new ArrayList<>(); 
		for(String title:titles) {
			titleList.add(title);
		}
		datas.add(titleList);
		
		for(Map<String,Object> lineMap:dataMaps) {
			List<String> lineDataList = new ArrayList<>(); 
			for(int i = 0;i<titles.length;i++) {
				String title = titles[i];
				lineDataList.add(StringUtil.parseString(lineMap.get(title)));
			}
			datas.add(lineDataList);
		}
		
		ExcelUtil.exportExcel(response, "洗车费报销记录"+DateUtil.curDayStr()+".xls", datas);
		
	}
	
	@RequestMapping({ "wyc/audit/export" })
	public void feeAuditList(HttpServletRequest request,HttpServletResponse response/*@RequestBody Map<String,Object> param*/) throws Exception {
		
		String titles [] = new String[] {"流程模板名称","审批状态","流程编号","申请人","申请部门","节点名称","操作者","操作人部门","操作类型","操作日期","操作时间","处理意见"};
		
		List<Map<String,Object>> dataMaps = personDAO.selectWYCFeeAuditBySql();
		
		List<List<String>> datas = new ArrayList<>();
		List<String> titleList = new ArrayList<>(); 
		for(String title:titles) {
			titleList.add(title);
		}
		datas.add(titleList);
		
		for(Map<String,Object> lineMap:dataMaps) {
			List<String> lineDataList = new ArrayList<>(); 
			for(int i = 0;i<titles.length;i++) {
				String title = titles[i];
				lineDataList.add(StringUtil.parseString(lineMap.get(title)));
			}
			datas.add(lineDataList);
		}
		
		ExcelUtil.exportExcel(response, "网约车报销审批记录"+DateUtil.curDayStr()+".xls", datas);
		
	}
	
	private String getClientIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if(!StringUtil.isBlank(ip) && !"unKnown".equalsIgnoreCase(ip)){
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = ip.indexOf(",");
            if(index != -1){
                return ip.substring(0,index);
            }else{
                return ip;
            }
        }
        ip = request.getHeader("X-Real-IP");
        if(!StringUtil.isBlank(ip) && !"unKnown".equalsIgnoreCase(ip)){
            return ip;
        }
        return request.getRemoteAddr();
    }

}
